package com.lh.api.controller.index;

import com.lh.api.controller.AbstractController;
import com.lh.api.service.ShopService;
import com.lh.api.service.WinningAnnouncementService;
import com.lh.api.vo.SearchVo;
import com.lh.api.vo.WinningVo;
import com.lh.common.enums.ShopSortTypeEnum;
import com.lh.common.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Api(value="IndexAction",tags={"首页相关 v1.3"})
@RequestMapping("/api/index")
public class IndexAction extends AbstractController {

    @Autowired
    private ShopService shopService;

    @Autowired
    private WinningAnnouncementService winningAnnouncementService;

    @ApiOperation(value = "搜索 v1.3")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "sortType", value = "商品排序类型：DEFAULT:默认 时间排序，PRICEDESC:价格从高到底，PRICEASC:价格从低到高 v1.3", required = false, dataTypeClass = ShopSortTypeEnum.class),
            @ApiImplicitParam(paramType="query", name = "condition", value = "搜索条件", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "当前页", required = true,dataTypeClass= Integer.class),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "页数大小", required = true,dataTypeClass= Integer.class)
    })
    @GetMapping("/search")
    @ResponseBody
    public Result<SearchVo> search(String condition, ShopSortTypeEnum sortType, Integer pageNum, Integer pageSize){
        AssertUtil.assertNotNull(condition, Constant.RESULT.FI1000, "Condition. is required");
        if(StringUtils.isBlank(condition)){
            return Result.ok(new PageUtils<>(new ArrayList<>()));
        }
        Map<String, Object> params = new HashMap<>();
        params.put("condition", condition);
//        PageUtils<SearchVo> searchVoPageUtils = shopService.searchObj(params, pageNum, pageSize);
        PageUtils<SearchVo> searchVoPageUtils = shopService.searchObj(params, sortType, pageNum, pageSize);
        return Result.ok(searchVoPageUtils);
    }

    @ApiOperation(value = "查询所有中奖播报信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "qureyTime", value = "查询时间,形如:2020-03-01 21:00:00,查询第一页时不用传，由后端返回", required = false,dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "当前页", required = true,dataTypeClass= Integer.class),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "页数大小", required = true,dataTypeClass= Integer.class)
    })
    @GetMapping("/getWinningAnnouncementList")
    @ResponseBody
    public Result<PageUtils<WinningVo>> getWinningAnnouncementList(String qureyTime,Integer pageNum, Integer pageSize) {
        if(pageNum != null && pageNum == 1){
            qureyTime = DateTimeUtil.getDateTime();
        }
//        else {
//            AssertUtil.assertNotNull(qureyTime, Constant.RESULT.FI1000, "QureyTime is required");
//        }
        PageUtils<WinningVo> winningVoPageUtils = winningAnnouncementService.getList(qureyTime,pageNum, pageSize);
        winningVoPageUtils.setQureyTime(qureyTime);
        return Result.ok(winningVoPageUtils);
    }


    @ApiOperation(value = "首页商品列表")
    @GetMapping(value = "/shopList")
    @ResponseBody
    public Result<List<SearchVo>> indexShop(){
        List<SearchVo> searchVos = shopService.getIndexList();
        return Result.ok(searchVos);
    }

}
